import  re
# iliega_word = ['暴力','攻击']
# word_regex = re.compile(r'暴力|攻击|非法')
# message = '暴力的人是具有攻击性的是非法的'
# result = word_regex.sub('*****',message)
# print(result)




# phone_number = '123-456-789'
# phone_number = re.sub('\D','',phone_number)
# print(phone_number)


'''
思路:使用使用正则表达式匹配字符串中的数字 然后使用findall方法 

'''
# s = 'A123B34CD233'
# s_regex = re.compile(r'[A-Z]*(\d+)[A-Z]*(\d+)[A-Z]*(\d+)')
# result = s_regex.findall(s)
# for element in result:
#     for i in element:
#          s =s.replace(i,str(int(i)*3))
# print(s)
#http://123.abc.com/qwerty.js     http://www.abc.com/asdfgh.js?version=1   http://\d*www.abc.com/[a-z]+.js\??\D*\d?]
# str = 'http://www.abc.com/asdfgh.js?version=1'
# str01 = 'http://123.abc.com/qwerty.js'
# url_regex = re.compile(r'http://\S+.abc.com/[a-z]+.js\S*') #\S表示非空格
# result = url_regex.match(str)
# result02 = url_regex.match(str01)
# print("第一个网站:"+result.group())
# print("第二个网站:"+result02.group())


import re
import os
import sys
import urllib.request
BOR_amount=0.0
p_path=sys.path[0]  #获取当前路径
url='file:'+p_path+'\moviesample.html'     #得到网页所在路径
req = urllib.request.Request(url, headers={'User-Agent' : "Magic Browser"})
webpage= urllib.request.urlopen(req)
strw = webpage.read().decode("utf-8")
s = strw.find("电影名称</th><th>总场次/占比")
e=strw[s:].find("以上数据仅供参考")
strw_table=strw[s:s+e]
reStr = """<tr class="[a-z]{3,4}"><td><a href="http://somemovies.com/film/[0-9]+/boxoffice" title=.+</tr>"""
m = re.findall(reStr,strw_table)
if not m:
   os._exit(0)
for t in m:
   ss = re.findall(r'(\d+[\.]?\d*[%]?[^\x00-\xff]*)',t)
   if ss:
       print(ss)
       BOR_amount+= float(ss[-3].replace('万',''))
   else:
      print("出错了！")
print("票房总额是:  "+str(BOR_amount))